<!--
 * @Description: 
 * @Author: ljl
 * @Date: 2024-11-15 08:36:29
 * @LastEditors: rendc
 * @LastEditTime: 2024-11-16 21:35:01
-->
<!-- <template>
  <el-button>测试按钮</el-button>
  <el-button type="primary" :icon="Edit" />
  <el-button type="primary" :icon="Share" />
  <el-button type="primary" :icon="Delete" />
  <el-button type="primary" :icon="Search">Search</el-button>
</template>


<script setup>
  import { Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
</script>


<style scoped>

</style> -->
<template>
  <div class="main">
    <el-row>
        <el-button link type="primary" size="large" @click="handleAdd">新增</el-button>
      <el-col :span="4">
        <el-input placeholder="请输入学号或姓名" v-model="search"></el-input>      
      </el-col>
    </el-row>

    <el-table :data="filteredData" border style="width: 70%">
      <el-table-column prop="id" label="学号" width="180" />
      <el-table-column prop="name" label="姓名" width="180" />
      <el-table-column prop="score" label="分数" width="180" />
      <el-table-column prop="desc" label="评语" />
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">  删除  </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑模态框 开始 -->
    <el-dialog v-model="dialogFormVisible" title="编辑" width="500">
      <el-form :model="form">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="form.id" disabled autocomplete="off" />
        </el-form-item> 
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="form.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="form.desc" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="editUser">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 新增 -->
    <el-dialog v-model="addDialogVisible" title="新增" width="500">
      <el-form :model="newform">
        <el-form-item label="id" :label-width="formLabelWidth">
          <el-input v-model="newform.id" autocomplete="off" />
        </el-form-item> 
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="newform.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分数" :label-width="formLabelWidth">
          <el-input-number :min="0" :max="100" v-model="newform.score" autocomplete="off" />
        </el-form-item>
        <el-form-item label="评语" :label-width="formLabelWidth">
          <el-input v-model="newform.desc" type="textarea" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addUser">
            确认
          </el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 编辑模态框 结束 -->
  </div>
</template>
<script setup>
import { ref,computed } from 'vue'
const tableData = ref([
  {
    id: '001',
    name: '张三',
    score: 90,
    desc: "很优秀"
  },
  {
    id: '002',
    name: '李四',
    score: 80,
    desc: "优秀"
  },
  {
    id: '003',
    name: '王五',
    score: 70,
    desc: "良好"
  },
])
const formLabelWidth = '140px'
const form = ref({});
// 编辑的模态框
const dialogFormVisible = ref(false);
const newform = ref({});
// 新增的模态框相关
const addDialogVisible = ref(false);
// 查询输入框绑定的响应式数据
const search = ref('')

const filteredData = computed(() => {
  if (!search.value) {
    return tableData.value;
  }
  return tableData.value.filter(item =>
    Object.values(item).some(value =>
      String(value).toLowerCase().includes(search.value.toLowerCase())
    )
  );
});
function handleDelete (id) {
  // 发送请求 delStudentById(id);
  tableData.value.forEach((e, index) => {
    if (e.id == id) {
      // 根据下标删除数组元素
      tableData.value.splice(index, 1);
    }
  })
}
function handleAdd() {
  newform.value = {
    id: '',
    name: '',
    score: '',
    desc: ''
  };
  addDialogVisible.value = true;
}
// 打开编辑表单
function handleEdit (row) {
  form.value = row;
  dialogFormVisible.value = true
}
// 提交编辑表单
function editUser () {
  // 将数据发送到后台
  // editStudentById(form.value);
  dialogFormVisible.value = false
}
function addUser () {
  // 将数据发送到后台
  tableData.value.push(newform.value);
  addDialogVisible.value = false
}
</script>

<style scoped>
.main {
  height: 100%;
  /* text-align: center; */
  /* border: 1px solid red; */
  padding-left: 25%;
  padding-top: 15%;
}

/* 表格样式 */
.el-table {
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
h1 {
  text-align: center;
}
</style>
